package com.itheima.reggie.service.impl;

import cn.hutool.core.math.MathUtil;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.JwtUtil;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.common.SmsTemplate;
import com.itheima.reggie.domain.User;
import com.itheima.reggie.mapper.UserMapper;
import com.itheima.reggie.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private SmsTemplate smsTemplate;

    @Autowired
    private UserMapper userMapper;

    @Override
    public void sendSms(String phone) {
        //生成6位数验证码
        //String code = RandomUtil.randomNumbers(6);

        String code = "123";
        System.out.println("生成的验证码是:" + code);

        //调用reids保存一下
        redisTemplate.opsForValue().set("CODE_" + phone, code, 5, TimeUnit.MINUTES);

        //调用阿里云发送
        //smsTemplate.sendSms(phone, code);
    }

    //登录
    @Override
    public ResultInfo login(String phone, String code) {
        //比对验证码, 如果失败返回错误提示
        String codeFromRedis = (String) redisTemplate.opsForValue().get("CODE_" + phone);
        if (!StringUtils.equals(code,codeFromRedis)){
            return ResultInfo.error("验证码错误");
        }
        //根据手机号查询用户是否存在
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getPhone,phone);
        User user = userMapper.selectOne(wrapper);
        //如果查询到了,登录成功
        if (user != null){
            if (user.getStatus() != 1){
                return ResultInfo.error("当前用户已被禁用");
            }
        }else {
           user = new User();
           user.setPhone(phone);
           user.setStatus(1);
           userMapper.insert(user);
        }
        //生成token
        Map map = new HashMap();
        map.put("id",user.getId());
        String token = JwtUtil.createToken(map);
        //将token和用户信息保存到redis
        redisTemplate.opsForValue().set("TOKEN_" + token,user,1,TimeUnit.DAYS);
        //返回token
        return ResultInfo.success(token);
    }

    @Override
    public void remove(String token) {
        redisTemplate.delete("TOKEN_" + token);
    }
}
